<template>
  <div class="page">
    <!-- 头部 -->
    <header>
      <h3>收藏</h3>
    </header>
    <!-- 导航栏 -->
    <nav>
      <li
        v-for="(item, index) in navList"
        :key="index"
        :class="{ active: activeIndex == index }"
        @click="acitveAction(index)"
      >
        {{ item }}
      </li>
      <!-- 点击增加收藏夹 -->
      <i
        class="iconfont icon-jiajianzujianjiahao"
        type="primary"
        text="显示遮罩层"
        @click="show = true"
      ></i>
    </nav>
    <!-- 遮罩层 -->
    <van-overlay :show="show" @click="show = false">
      <div class="wrapper">
        <div class="block" @click.stop>
          <h3>创建收藏夹</h3>
          <p>请给收藏夹取一个好听的名字吧</p>
          <input type="text" placeholder="请输入内容" v-model="value" />
          <div class="btn">
            <button @click="unsureAction">取消</button>
            <button class="btn2" @click="sureAction">确定</button>
          </div>
        </div>
      </div>
    </van-overlay>
    <!-- 点击导航展示的对应内容 -->
    <component :is="comName" class="content" :list="list" />
  </div>
</template>

<script>
import Wufenlei from "./com/wufenlei.vue";
import Shoucang from "./com/shoucang/index.vue";
import Tuijian from "./com/tuijian.vue";
import { Toast } from "vant";
import { addCollectApi } from "@/api/collect";
import { findCollectApi } from "@/api/collect";
export default {
  components: {
    Wufenlei,
    Shoucang,
    Tuijian,
  },
  data() {
    return {
      value: "",
      navList: ["无分类", "收藏夹", "随机推荐"],
      activeIndex: 0,
      show: false,
      list: [],
    };
  },
  computed: {
    comName() {
      switch (this.activeIndex) {
        case 0:
          return "Wufenlei";
        case 1:
          return "Shoucang";
        case 2:
          return "Tuijian";
      }
    },
  },
  methods: {
    acitveAction(index) {
      // 切换导航
      this.activeIndex = index;
    },
    getContainer() {
      return document.querySelector(".btn2");
    },
    unsureAction() {
      this.show = false;
    },
    async sureAction() {
      try {
        let res = await addCollectApi({
          collect_name: this.value,
        });
        Toast({
          message: res.data.message,
        });
        this.show = false;
        // console.log(res);
      } catch (error) {}
      // 所有收藏夹
      this.findColflect();
    },
    async findColflect() {
      try {
        let res = await findCollectApi();
        this.list = res.data.data;
      } catch (error) {}
    },
  },
  created() {
    this.findColflect();
  },
};
</script>

<style scoped lang="scss">
.page {
  z-index: 0;
  display: flex;
  flex-direction: column;
  header {
    height: 50px;
    line-height: 50px;
    h3 {
      text-align: center;
      font-weight: 700;
    }
  }
  nav {
    display: flex;
    padding: 10px 20px;
    li {
      line-height: 30px;
      padding-right: 20px;
      font-size: 16px;
      color: #999;
    }
    .active {
      color: #000;
    }
    i {
      line-height: 30px;
      position: absolute;
      right: 20px;
    }
  }
  .content {
    flex: 1;
    padding: 10px 20px;
    z-index: 1;
  }
  .van-overlay {
    z-index: 10;
  }

  .wrapper {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 100%;
  }
  .block {
    width: 260px;
    height: 200px;
    background-color: #fff;
    border-radius: 10px;
    text-align: center;
    position: relative;
    h3 {
      font-weight: 700;
      line-height: 50px;
    }
    p {
      line-height: 30px;
      font-size: 14px;
      color: #6d6a6a;
    }
    input {
      height: 40px;
      border: 1px solid #ccc;
      padding: 0 10px;
      border-radius: 5px;
      margin-top: 10px;
      &::placeholder {
        color: #ccc;
        font-size: 14px;
      }
    }
    .btn {
      width: 100%;
      position: absolute;
      bottom: 0;
      left: 0;
      button {
        width: 50%;
        height: 50px;
        background-color: #fff;
        border: 1px solid #ccc;
        color: #ccc;
        border-radius: 0 0 0 5px;
        &:last-child {
          color: blue;
          border-left: none;
          border-radius: 0 0 5px 0;
        }
      }
    }
  }
}
</style>
